package com.myTest.utils;

import com.myTest.annotation.Autowired;
import com.myTest.annotation.Component;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
@Component("connectionUtils")
public class ConnectionUtils {


    @Autowired
    private DataSource dataSource;

    private ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();

    public Connection getCurrentThreadConn() throws SQLException {

        /**
         * 判断当前线程中是否已经绑定连接，如果没有绑定，需要从连接池获取⼀个连接绑定到当前线程
         */
        Connection connection = threadLocal.get();
        if(connection==null){
            // 从连接池拿连接并绑定到线程
            connection = dataSource.getConnection();
            // 绑定到当前线程
            threadLocal.set(connection);
        }
        return connection;
    }
}
